package com.online.usermanagesystem.dao;

import com.online.usermanagesystem.entity.Account;
import com.online.usermanagesystem.utils.UpdateLangDriver;
import org.apache.ibatis.annotations.*;

import java.util.List;

/**
 * @author jhk
 */

@Mapper
public interface AccountMapper {

    @Select("select name,nickname,avatar from ols_account where id=#{id}")
    Account query(Integer id);

    @Select("select id,nickname,avatar from ols_account where name=#{name} and password=#{password}")
    Account auth(String name, String password);

    @Select("select count(*) from ols_account where id=#{id}")
    Integer countById(Integer id);

    @Select("select count(*) from ols_account where name=#{name}")
    Integer countByName(String name);

    @Insert("insert into ols_account(name,password,nickname,avatar,time_create,config) values(#{name},#{password},#{nickname},#{avatar},now(),'{}')")
    @Options(useGeneratedKeys = true)
    Integer insert(Account account);

    //@Update("update ols_account set nickname=#{nickname},avatar=#{avatar}) where id=#{id})")
    @Update("update ols_account(#{account}) WHERE id = #{id}")
    @Lang(UpdateLangDriver.class)
    Integer update(Account account);

    @Select("select A.id,name,nickname,avatar from ols_account as A,(select id from ols_account limit #{index},#{count}) as B where A.id=B.id")
    List<Account> querys(@Param(value = "index") Integer index, @Param(value = "count") Integer count);

    @Select("select A.id,name,nickname,avatar from ols_account as A,(select id from ols_account where nickname like #{keyword} limit #{index},#{count}) as B where A.id=B.id")
    List<Account> search(@Param(value = "keyword") String keyword, @Param(value = "index") Integer index, @Param(value = "count") Integer count);
}
